package org.parademo.web.controller.util;

import org.parademo.model.AddressEntity;
import org.parademo.model.UserEntity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class ViewFormatterUtil {
    private ViewFormatterUtil() {
    }

    public static String formatBoolean(Boolean aBoolean) {
        if (aBoolean == null) {
            return formatNull();
        }

        return aBoolean ? "yes" : "no";
    }

    public static String formatNull() {
        return "";
    }

    public static String formatAddress(AddressEntity address) {
        return address.getStreetName() + " " + address.getHouseNumber() + ", " +
                address.getZipCode() + ", " +
                address.getCity() + ", " +
                address.getCountry().getCountryName();
    }

    public static String formatDateToString(Date date) {

        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
        return format.format(date);
    }

    public static Date formatStringToDate(String dateStr) {

        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

        try {

            return (Date)format.parse(dateStr);

        } catch (ParseException e) {

            //TODO: Change this!
            throw new RuntimeException("ViewFormatterUtil.formatStringToDate :: Incorrect date format String provided! : "  + e.getMessage());
        }
    }

    public static String formatUser(UserEntity user) {
        return user.getFirstName() + " " + user.getLastName() + " (" + user.getUserName() + ")";
    }
}
